using System;
using System.Collections.Generic;
using System.IO;
using com.echonest.api.v4;
using jENIKVMApi;

namespace Examples
{
    public class SearchSongsExample
    {
        private EchoNestAPI en;


        public SearchSongsExample()
        {
            en = new EchoNestAPI(File.ReadAllText("developer_key.txt"));
            en.setTraceSends(false);
            en.setTraceRecvs(false);
        }

        private void smallDumpSong(Song song)
        {
            Console.Write("{0}\n", song.getTitle());
            Console.Write("   artist: {0}\n", song.getArtistName());
        }

        public virtual void dumpSong(Song song)
        {
            Console.Write("{0}\n", song.getTitle());
            Console.Write("   artist: {0}\n", song.getArtistName());
            Console.Write("   dur   : {0:F3}\n", song.getDuration());
            Console.Write("   BPM   : {0:F3}\n", song.getTempo());
            Console.Write("   Mode  : {0:D}\n", song.getMode());
            Console.Write("   S hot : {0:F3}\n", song.getSongHotttnesss());
            Console.Write("   A hot : {0:F3}\n", song.getArtistHotttnesss());
            Console.Write("   A fam : {0:F3}\n", song.getArtistFamiliarity());
            Console.Write("   A loc : {0}\n", song.getArtistLocation());
        }


        public virtual void searchSongsByArtist(string artist, int results)
        {

            SongParams p = new SongParams();
            p.setArtist(artist);
            //p.add("bucket", "audio_summary");
            //p.add("bucket", "tracks");
            //p.add("bucket", "artist_hotttnesss");
            p.includeAudioSummary();
            //p.includeTracks();
            p.includeArtistHotttnesss();
            p.setResults(results);

            IList<Song> songs = en.SearchSongs(p);
            foreach (Song song in songs)
            {
                dumpSong(song);
                Console.WriteLine();
            }
        }

        private void identifyByFingerprint()
        {   
            SongParams p = new SongParams();
            string fingerprint = "eJxVlIuNwzAMQ1fxCDL133-xo1rnGqNAEcWy_ERa2aKeZmW9ustWVYrXrl5bthn_laFkzguNWpklEmoTB74JKYZSPlbJ0sy9fQrsrbEaO9W3bsbaWOoK7IhkHFaf_ag2d75oOQSZczbz5CKA7XgTIBIXASvFi0A3W8pMUZ7FZTWTVbujCcADlQ_f_WbdRNJ2vDUwSF0EZmFvAku_CVy440fgiIvArWZZWoJ7GWd-CVTYC5FCFI8GQdECdROE20UQfLoIUmhLC7IiByF1gzbAs3tsSKctyC76MPJlHRsZ5qhSQhu_CJFcKtW4EMrHSIrpTGLFqsdItj1H9JYHQYN7W2nkC6GDPjZTAzL9dx0fS4M1FoROHh9YhLHWdRchQSd_CLTpOHkQQP3xQsA2-sLOUD7CzxU0GmHVdIxh46Oide0NrNEmjghG44Ax_k2AoDHsiV6WsiD6OFm8y-0Lyt8haDBBzeMlAnTuuGYIB4WA2lEPAWbdeOabgFN6TQMs6ctLA5fHyKMBB0veGrjPfP00IAlWNm9n7hEh5PiYYBGKQDP-x4F0CL8HkhoQnRWN997JyEpnHFR7EhLPQMZmgXS68hsHktEVErranvSSR2VwfJhQCnkuwhBUcINNY-xu1pmw3PmBqU9-8xu0kiF1ngOa8vwBSSzzNw==";
            p.add("code", fingerprint);

            IList<Song> songs = en.IdentifySongs(p);
            foreach (Song song in songs)
            {
                smallDumpSong(song);
                Console.WriteLine();
            }
        }



        public virtual void searchSongsByTempo(string artist, int results)
        {
            SongParams p = new SongParams();
            p.add("artist", artist);
            p.add("bucket", "audio_summary");
            p.setResults(results);
            p.add("sort", "tempo-asc");
           
            IList<Song> songs = en.SearchSongs(p);
            foreach (Song song in songs)
            {
                Console.Write("{0:F0} {1} {2}\n", song.getTempo(), song.getArtistName(), song.getTitle());
            }
        }


        public virtual void searchSongsByTitle(string title, int results)
        {
            SongParams p = new SongParams();
            p.setTitle(title);
            p.setResults(results);
            IList<Song> songs = en.SearchSongs(p);
            foreach (Song song in songs)
            {
                dumpSong(song);
                Console.WriteLine();
            }
        }

        public virtual void stats()
        {
            en.showStats();
        }



        public static void Run()
        {
           SearchSongsExample sse = new SearchSongsExample();
            sse.searchSongsByArtist("weezer", 10);
            sse.searchSongsByTempo("The Beatles", 100);
            sse.identifyByFingerprint();
            sse.stats();
        }


    }
}